var ClubViewUtil = {};
ClubViewUtil.ClubTableWindow = null;
ClubViewUtil.ClubMainWindow = null;

ClubViewUtil.cacheWindow = function (windowName) {
    var originalName = windowName;
    if (window[windowName + '_' + app.classExtendSuffix]) {
        windowName += '_' + app.classExtendSuffix;
    }
    if (!window[windowName]) {
        cc.error('想要显示的视窗 : ' + windowName + '不存在');
        return;
    }
    var arg = arguments;
    var win = new window[windowName](_.drop(arg));
    if (!(win instanceof Window)) {
        cc.error('想要显示的不是一个视窗 :' + windowName);
        return;
    }
    win.initView.apply(win, _.drop(arg));
    win.setName(originalName);
    //如果这个字段是true  在showWindow的时候 会保存这个window的name，
    //断线重现后会检测如果有保存的name自动打开对应的window
    if (win.recordWindowName) {
        lastViewWindow = {};
        lastViewWindow.windowName = windowName;
        lastViewWindow.value = _.drop(arg);
    }
    return win;
};

//亲友圈选择界面
ClubViewUtil.cacheClubMainWindow = function () {
    if (ClubViewUtil.ClubMainWindow) return;
    ClubViewUtil.ClubMainWindow = ClubViewUtil.cacheWindow('ClubMainWindow');
    ClubViewUtil.ClubMainWindow.retain();
};
ClubViewUtil.openClubMainWindow = function () {
    if (!ClubViewUtil.ClubMainWindow || !cc.sys.isObjectValid(ClubViewUtil.ClubMainWindow)) {
        ClubViewUtil.cacheClubMainWindow();
    }
    if (window.curScene && cc.sys.isObjectValid(window.curScene)) {
        window.curScene.addChild(ClubViewUtil.ClubMainWindow);
    }
    ClubViewUtil.ClubMainWindow.open();
};

//亲友圈牌桌界面
ClubViewUtil.cacheClubTableWindow = function () {
    if (ClubViewUtil.ClubTableWindow) return;
    ClubViewUtil.ClubTableWindow = ClubViewUtil.cacheWindow('ClubTableWindow');
    ClubViewUtil.ClubTableWindow.retain();
};
ClubViewUtil.openClubTableWindow = function (clubId) {
    if (!window.curScene || !cc.sys.isObjectValid(window.curScene)) {
        return;
    }
    if (!ClubViewUtil.ClubTableWindow || !cc.sys.isObjectValid(ClubViewUtil.ClubTableWindow)) {
        ClubViewUtil.cacheClubTableWindow();
    }

    if (ClubViewUtil.ClubTableWindow && cc.sys.isObjectValid(ClubViewUtil.ClubTableWindow) && ClubViewUtil.ClubTableWindow.getParent()) {
        var board = window.curScene.getChildByTag(BoardTag);
        ClubViewUtil.ClubTableWindow.setLocalZOrder(board.getLocalZOrder() + 1);
    } else {
        window.curScene.addChild(ClubViewUtil.ClubTableWindow);
        ClubViewUtil.ClubTableWindow.resetAllListeners();
    }
    ClubViewUtil.ClubTableWindow.open(clubId);
};


/**
 * 显示亲友圈界面
 * @param joinId
 */
ClubViewUtil.showClubMainWindow = function (joinId) {
    if (joinId) {
        if (ClubDataManager.ClubRecordData) {
            // showOldWindow('ClubStatisticsLayer', joinId);
            ClubViewUtil.openClubTableWindow(joinId);
            showWindow('ClubStatisticsWindow', joinId,'zhanji');
            club.openHistoryDetail(this, ClubDataManager.ClubRecordData);
        } else {
            if (ClubDataManager.getClubData(joinId)) {
                ClubViewUtil.openClubTableWindow(joinId);
                playMusic('bgMusic');
            } else {
                ClubDataManager.RooomReturnClubId = joinId;
                //断线重连 返回亲友圈时  没有俱乐部数据。需要请求一下
                clubMsgManager.requestClubList(true);
            }
        }

    } else {
        ClubViewUtil.openClubMainWindow();
    }
};

ClubViewUtil.hideClubTableWindow = function () {
    if (!window.curScene || !cc.sys.isObjectValid(window.curScene)) {
        return;
    }
    if (ClubViewUtil.ClubTableWindow && cc.sys.isObjectValid(ClubViewUtil.ClubTableWindow) && ClubViewUtil.ClubTableWindow.getParent()) {
        ClubViewUtil.ClubTableWindow.setLocalZOrder(0);
    }
};

ClubViewUtil.clearCache = function () {
    if (ClubViewUtil.ClubMainWindow) {
        ClubViewUtil.ClubMainWindow.release();
        ClubViewUtil.ClubMainWindow = null;
    }

    if (ClubViewUtil.ClubTableWindow) {
        ClubViewUtil.ClubTableWindow.release();
        ClubViewUtil.ClubTableWindow = null;
    }

};

ClubViewUtil.cacheClub = function () {
    var handler = function () {
        ClubViewUtil.cacheClubMainWindow();
        ClubViewUtil.cacheClubTableWindow();
    };
    cc.loader.load(club.res.ClubMainWindow_json, handler, null, true);
};